package com.longcai.cm.controller;

import cn.hutool.core.date.DateUtil;
import com.longcai.cm.base.aop.Log;
import com.longcai.cm.base.bean.PageVo;
import com.longcai.cm.base.bean.Pageable;
import com.longcai.cm.bean.R;
import com.longcai.cm.common.utils.RequestUtil;
import com.longcai.cm.common.utils.StringUtil;
import com.longcai.cm.domain.VMemberVipList;
import com.longcai.cm.excel.ExcelUtils;
import com.longcai.cm.service.VMemberVipListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author LBX
 * @date 2024-10-18
 */
@RestController
@RequestMapping("/VMemberVipList")
public class VMemberVipListController {

    private final VMemberVipListService vMemberVipListService;

    public VMemberVipListController(VMemberVipListService vMemberVipListService) {
        this.vMemberVipListService = vMemberVipListService;
    }

    @RequestMapping("/getList")
    public R<PageVo<VMemberVipList>> getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        String type = request.getParameter("type");
        if (type.equals("1")) {
            pageInfo.getPageSearch().put("SEARCH_LE_expire_time", DateUtil.now());
        } else if (type.equals("2")) {
            pageInfo.getPageSearch().put("SEARCH_GE_expire_time", DateUtil.now());
        }
        PageVo<VMemberVipList> result = vMemberVipListService.getPageBean(pageInfo, VMemberVipList.class);
        return R.success(result);
    }

    /**
     * @param vo 插入实体
     * @return R
     */
    @Log
    @RequestMapping("/save")
    public R<String> save(VMemberVipList vo) {
        if (StringUtils.isNotBlank(vo.getId())) {
            boolean success = vMemberVipListService.updateById(vo);
            return success ? R.success() : R.error();
        } else {
            final String uuid = StringUtil.getUuid();
            vo.setId(uuid);
            boolean success = vMemberVipListService.save(vo);
            return success ? R.success() : R.error();
        }
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return R
     */
    @Log
    @RequestMapping("/edit")
    public R<String> edit(VMemberVipList vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            boolean success = vMemberVipListService.updateById(vo);
            return success ? R.success() : R.error();
        } else {
            return R.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return R
     */
    @Log
    @RequestMapping("/delete/{id}")
    public R<String> deleteByPrimaryKey(@PathVariable("id") Integer id) {
        final boolean success = vMemberVipListService.removeById(id);
        return success ? R.success() : R.error();
    }

    /**
     * 【API】---- 删除多个数据
     *
     * @return R
     */
    @Log
    @RequestMapping("/delete")
    public R<String> delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            final boolean success = vMemberVipListService.removeById(idString);
            if (!success) {
                return R.error();
            }
        }
        return R.success();
    }


    @Log
    @RequestMapping("toExcel")
    public void toExcel(HttpServletRequest request, HttpServletResponse response, String ids){
        List<VMemberVipList> jaOrders ;
        if (StringUtils.isNotBlank(ids)){
            String[] split = ids.split(",");
            List<String> list = Arrays.asList(split);
            jaOrders =  vMemberVipListService.listByIds(list);
        }else{
            jaOrders = vMemberVipListService.list();
        }
        ExcelUtils.export(response,"会员表"+ DateUtil.today(),jaOrders,VMemberVipList.class);
    }


}
